<template>
	<view class="qh-bottom-button flex-cn-cn" :class="{'fixed-bottom-button': fixedBottom}" :style="customStyles">
		<view v-if="$slots['prepend'] || prependText != ''" class="prepend flex-cn-cn" @click="prepend" :style="prependStyles">
			<template v-if="$slots['prepend']">
				<slot name="prepend"></slot>
			</template>
			<template v-else>
				{{prependText}}
			</template>
		</view>
		<view class="cancel flex-cn-cn" v-if="showCancel" @click="cancel" :style="cancelStyles">
			<template v-if="$slots['cancel']">
				<slot name="cancel"></slot>
			</template>
			<template v-else>
				{{cancelText}}
			</template>
		</view>
		<view class="confirm flex-cn-cn" @click="confirm" :style="confirmStyles">
			<template v-if="$slots['confirm']">
				<slot name="confirm"></slot>
			</template>
			<template v-else>
				{{confirmText}}
			</template>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'qh-bottom-button',
		props: {
			confirmText: {
				type: String,
				default: '确定'
			},
			cancelText: {
				type: String,
				default: '取消'
			},
			prependText: {
				type: String,
				default: ''
			}, //前置按钮文本，不设置或不存在$slots['prepend']时不显示前置按钮
			customStyle: {
				type: [String, Object],
				default () {
					return {}
				}
			},
			cancelStyle: {
				type: [String, Object],
				default () {
					return {}
				}
			},
			confirmStyle: {
				type: [String, Object],
				default () {
					return {}
				}
			},
			prependStyle: {
				type: [String, Object],
				default () {
					return {}
				}
			},
			showCancel: {
				type: Boolean,
				default: true,
			}, //是否显示取消按钮
			fixedBottom: {
				type: Boolean,
				default: false,
			}, //是否是固定在页面底部的样式，只会改变按钮边距及颜色，并不会使用position：fixed，推荐页面尽量使用flex布局
		},
		emit: ['cancel', 'confirm', 'prepend'],
		computed: {
			customStyles() {
				let str = '';
				if (this.fixedBottom && this.$store.bottomHeight > 0) {
					str = 'padding-bottom:' + (this.$store.bottomHeight + 20) + 'rpx;';
				}
				return str + this.$joinStyle(this.customStyle)
			},
			cancelStyles() {
				return this.$joinStyle(this.cancelStyle);
			},
			confirmStyles() {
				return this.$joinStyle(this.confirmStyle);
			},
			prependStyles() {
				return this.$joinStyle(this.prependStyle);
			},
		},
		methods: {
			prepend() {
				this.$emit('prepend');
			},
			cancel() {
				this.$emit('cancel');
			},
			confirm() {
				this.$emit('confirm');
			},
		},
	}
</script>

<style lang="scss">
	.qh-bottom-button {
		width: 100vw;
		padding: 72rpx 44rpx;
		box-sizing: border-box;

		>view {
			height: 80rpx;
			box-sizing: border-box;
			border-radius: 8rpx;
			font-size: 28rpx;
			line-height: 36rpx;
			color: #1E1E1E;

			&:active {
				opacity: 0.8;
			}
		}

		.prepend {
			width: 194rpx;
			margin-right: 20rpx;
		}

		.cancel {
			flex: 1;
			max-width: 224rpx;
			margin-right: 20rpx;
			border: 2rpx solid #EFF1F0;
			background-color: #FFFFFF;
			box-sizing: content-box;
		}

		.confirm {
			flex: 1;
			color: #FFFFFF;
			background-color: var(--primary-color);
		}

		&.fixed-bottom-button {
			background-color: #FFFFFF;
			padding: 28rpx 56rpx 20rpx 56rpx;
			box-shadow: 0 0 16rpx rgba(0, 0, 0, 0.05);
			position: relative;
			z-index: 1;

			.cancel {
				background-color: #F2F3F5;
				border: none;
			}
		}
	}
</style>
